package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /*新增套餐*/
    @Override
    public void insert(SetmealDTO dto) {
//        添加套餐对象到套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.insert(setmeal);
//        Long id = setmeal.getId();

//        添加套餐包括的菜品到套餐详情表
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (!setmealDishes.isEmpty() && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setMealDishMapper.inserts(setmealDishes);
        }


    }

    /*根据id查询套餐信息*/
    @Override
    public SetmealVO selectWithDishesById(Long setMealId) {
//        查询setmeal及类别名
        SetmealVO vo = setmealMapper.selectWithCateNameById(setMealId);
//        查询套餐包含的菜品
        List<SetmealDish> dishes = setMealDishMapper.getSetMealsBySetMealId(setMealId);
        vo.setSetmealDishes(dishes);
        return vo;
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void stopOrStart(Integer status, Long setMealId) {
//套餐内是否包含菜品
//        由套餐id获得菜品
        List<SetmealDish> setMealsByDishId = setMealDishMapper.getSetMealsBySetMealId(setMealId);
        if (setMealsByDishId.size() > 0 && !setMealsByDishId.isEmpty()) {
//            将套餐中对应的菜品Id取出
            List<Long> dishIds = new ArrayList<>();
            for (SetmealDish setmealDish : setMealsByDishId) {
                dishIds.add(setmealDish.getDishId());
            }
//            查询到套餐对应的菜品集合
//            List<Dish> dishes=dishMapper.getByIds(dishIds);
            List<Integer> statuses = dishMapper.getStatusByIds(dishIds);

            if (status.equals(StatusConstant.ENABLE)) {
//            判断套餐中的菜品是否有处于停售状态
                if (statuses.contains(StatusConstant.DISABLE))
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);

            }


        }
//            更改套餐的状态
        Setmeal setmeal = Setmeal.builder().id(setMealId).status(status).build();
        setmealMapper.update(setmeal);


    }

    @Override
    @Transactional
    public void deleteBatchByIds(List<Long> ids) {
//        起售中的套餐不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
//        删除套餐
        setmealMapper.deletes(ids);
//        套餐中的菜品记录需要删除
        setMealDishMapper.deletes(ids);
    }

    @Override
    @Transactional
    public void update(SetmealDTO dto) {
//        更新套餐的信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.update(setmeal);
//        更新套餐内包括的菜品信息
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
//        删除旧的信息
        setMealDishMapper.deleteBySetMealId(setmeal.getId());
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(dto.getId());
        });
//        插入新的菜品信息
        setMealDishMapper.inserts(setmealDishes);

    }

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

        /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
